#include "editor.h"
#include "common.h"
#include "game.h"
#include "func.h"

#include "target.h"
#include <unistd.h>

ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_EVENT_QUEUE *event_queue2;
ALLEGRO_TIMER *timer;

int gameloop(){

    al_start_timer(timer);

    Game *g = new Game();
    ALLEGRO_EVENT ev;

    bool doexit = false;

    while(!doexit){

        al_wait_for_event(event_queue, &ev);

        if(ev.type == ALLEGRO_EVENT_TIMER) {
             g->move_boxes();
             g->check_stage_complete();
        } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
             switch(ev.keyboard.keycode) {
                case ALLEGRO_KEY_UP:
                   g->move_player(UP);
                   break;

                case ALLEGRO_KEY_DOWN:
                   g->move_player(DOWN);
                   break;

                case ALLEGRO_KEY_LEFT:
                   g->move_player(LEFT);
                   break;

                case ALLEGRO_KEY_RIGHT:
                   g->move_player(RIGHT);
                   break;
                case ALLEGRO_KEY_F1:
                    g->restart();
                    break;
                case ALLEGRO_KEY_ESCAPE:
                   doexit = true;
                   break;
             }
       } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
            doexit = true;
       }
        al_set_target_bitmap(al_get_backbuffer(display));
        al_draw_bitmap(g->render(), 0, 0, 0);
        al_flip_display();
    }

    return 1;
}

int editorloop(){

    al_start_timer(timer);

    Editor *ed = new Editor();
    ALLEGRO_EVENT ev;

    bool doexit = false;

    while(!doexit){
        al_wait_for_event(event_queue2, &ev);

        if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
             switch(ev.keyboard.keycode) {
                case ALLEGRO_KEY_ESCAPE:
                   doexit = true;
                   break;
             }
       } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
            doexit = true;
       } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
              ed->onClick(ev.mouse.x, ev.mouse.y, ev.mouse.button);
       }

        al_set_target_bitmap(al_get_backbuffer(display));
        al_draw_bitmap(ed->render(), 0, 0, 0);
        al_flip_display();
    }

    return 1;
}


int main(int argc, char **argv){

   if(!init()) return -1;

    char opt;
    char mode = 'N';
	while ((opt = getopt(argc, argv, "m:ruh")) != -1){
		switch (opt){
			case 'm':
				sscanf(optarg, "%c", &mode);
				break;
		}
	}

	if(mode == 'N'){
        std::cout << "(E)ditor ou (G)ame ? ";
        std::cin >> mode;
	}

    std::cout << mode << std::endl;
    if(mode=='E' || mode == 'e'){
        editorloop();
    }else{
        gameloop();
    }

   al_destroy_display(display);
   al_destroy_timer(timer);
   al_destroy_event_queue(event_queue);

   return 0;
}

